package cn.wolfcode.p2p.business.service.impl;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.business.domain.AccountFlow;
import cn.wolfcode.p2p.business.domain.Bid;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.domain.CreditTransfer;
import cn.wolfcode.p2p.business.domain.MoneyWithdraw;
import cn.wolfcode.p2p.business.domain.PaymentSchedule;
import cn.wolfcode.p2p.business.domain.PaymentScheduleDetail;
import cn.wolfcode.p2p.business.domain.RechargeOffline;
import cn.wolfcode.p2p.business.mapper.AccountFlowMapper;
import cn.wolfcode.p2p.business.service.IAccountFlowService;

@Service
@Transactional
public class AccountFlowServiceImpl implements IAccountFlowService {

	@Autowired
	private AccountFlowMapper flowMapper;

	private AccountFlow createBaseFlow(Account account) {
		AccountFlow flow = new AccountFlow();
		flow.setAccountId(account.getId());
		flow.setActionTime(new Date());

		flow.setFreezedAmount(account.getFreezedAmount());
		flow.setUsableAmount(account.getUsableAmount());
		return flow;
	}

	@Override
	public void createRechargeOfflineFlow(Account account, RechargeOffline ro) {
		AccountFlow flow = this.createBaseFlow(account);
		flow.setActionType(Constants.ACCOUNT_ACTIONTYPE_RECHARGE_OFFLINE);
		flow.setAmount(ro.getAmount());
		flow.setNote("线下充值成功,充值金额为:" + ro.getAmount());
		this.flowMapper.insert(flow);
	}

	@Override
	public void createBidFlow(Account account, Bid bid) {
		AccountFlow flow = this.createBaseFlow(account);
		flow.setActionType(Constants.ACCOUNT_ACTIONTYPE_BID_FREEZED);
		flow.setAmount(bid.getAvailableAmount());
		flow.setNote("针对借款" + bid.getBidRequestTitle() + ",投标:" + bid.getAvailableAmount());
		this.flowMapper.insert(flow);
	}

	@Override
	public void createBidFaildFlow(Account account, Bid bid) {
		AccountFlow flow = this.createBaseFlow(account);
		flow.setActionType(Constants.ACCOUNT_ACTIONTYPE_BID_UNFREEZED);
		flow.setAmount(bid.getAvailableAmount());
		flow.setNote("借款" + bid.getBidRequestTitle() + "满审拒绝,投标:" + bid.getAvailableAmount() + "金额返还");
		this.flowMapper.insert(flow);
	}

	@Override
	public void createBidSuccessFlow(Account account, Bid bid) {
		AccountFlow flow = this.createBaseFlow(account);
		flow.setActionType(Constants.ACCOUNT_ACTIONTYPE_BID_SUCCESSFUL);
		flow.setAmount(bid.getAvailableAmount());
		flow.setNote("借款" + bid.getBidRequestTitle() + "借款成功,投标:" + bid.getAvailableAmount() + "成功");
		this.flowMapper.insert(flow);
	}

	@Override
	public void createBorrowSuccessFlow(Account account, BidRequest br) {
		AccountFlow flow = this.createBaseFlow(account);
		flow.setActionType(Constants.ACCOUNT_ACTIONTYPE_BIDREQUEST_SUCCESSFUL);
		flow.setAmount(br.getBidRequestAmount());
		flow.setNote("借款" + br.getTitle() + "成功");
		this.flowMapper.insert(flow);
	}

	@Override
	public void createManageFeeFlow(Account account, BidRequest br, BigDecimal manageFee) {
		AccountFlow flow = this.createBaseFlow(account);
		flow.setActionType(Constants.ACCOUNT_ACTIONTYPE_CHARGE);
		flow.setAmount(manageFee);
		flow.setNote("借款" + br.getTitle() + "成功,支付借款手续费:" + manageFee);
		this.flowMapper.insert(flow);
	}

	@Override
	public void createWithdrawFlow(Account account, MoneyWithdraw mw) {
		AccountFlow flow = this.createBaseFlow(account);
		flow.setActionType(Constants.ACCOUNT_ACTIONTYPE_WITHDRAW_FREEZED);
		flow.setAmount(mw.getMoneyAmount());
		flow.setNote("申请提现,冻结金额:" + mw.getMoneyAmount());
		this.flowMapper.insert(flow);
	}

	@Override
	public void createWithdrawFaildFlow(Account account, MoneyWithdraw mw) {
		AccountFlow flow = this.createBaseFlow(account);
		flow.setActionType(Constants.ACCOUNT_ACTIONTYPE_WITHDRAW_UNFREEZED);
		flow.setAmount(mw.getMoneyAmount());
		flow.setNote("申请提现拒绝,返还冻结金额:" + mw.getMoneyAmount());
		this.flowMapper.insert(flow);
	}

	@Override
	public void createWithdrawSuccessFlow(Account account, MoneyWithdraw mw) {
		AccountFlow flow = this.createBaseFlow(account);
		flow.setActionType(Constants.ACCOUNT_ACTIONTYPE_WITHDRAW);
		BigDecimal amount = mw.getMoneyAmount().subtract(mw.getFee());
		flow.setAmount(amount);
		flow.setNote("申请提现成功,支付到用户账户金额:" + amount);
		this.flowMapper.insert(flow);
	}

	@Override
	public void createWithdrawFeeFlow(Account account, MoneyWithdraw mw) {
		AccountFlow flow = this.createBaseFlow(account);
		flow.setActionType(Constants.ACCOUNT_ACTIONTYPE_WITHDRAW_MANAGE_CHARGE);
		flow.setAmount(mw.getFee());
		flow.setNote("申请提现成功,支付提现手续费:" + mw.getFee());
		this.flowMapper.insert(flow);
	}

	@Override
	public void createReturnMoneyFlow(Account account, PaymentSchedule ps) {
		AccountFlow flow = this.createBaseFlow(account);
		flow.setActionType(Constants.ACCOUNT_ACTIONTYPE_RETURN_MONEY);
		flow.setAmount(ps.getTotalAmount());
		flow.setNote("借款" + ps.getBidRequestTitle() + "第" + ps.getMonthIndex() + "期还款成功,还款金额:" + ps.getTotalAmount());
		this.flowMapper.insert(flow);
	}

	@Override
	public void createReceiveMoneyFlow(Account account, PaymentScheduleDetail psd) {
		AccountFlow flow = this.createBaseFlow(account);
		flow.setActionType(Constants.ACCOUNT_ACTIONTYPE_CALLBACK_MONEY);
		flow.setAmount(psd.getTotalAmount());
		flow.setNote("借款XX第" + psd.getMonthIndex() + "期回款成功,回款金额:" + psd.getTotalAmount());
		this.flowMapper.insert(flow);
	}

	@Override
	public void createInterestFeeFlow(Account account, PaymentScheduleDetail psd, BigDecimal interestFee) {
		AccountFlow flow = this.createBaseFlow(account);
		flow.setActionType(Constants.ACCOUNT_ACTIONTYPE_INTEREST_SHARE);
		flow.setAmount(interestFee);
		flow.setNote("借款回款成功,回款金额:" + psd.getTotalAmount() + ",支付利息管理费" + interestFee);
		this.flowMapper.insert(flow);
	}

	@Override
	public void createSubscribeFlow(Account account, CreditTransfer ct) {
		AccountFlow flow = this.createBaseFlow(account);
		flow.setActionType(Constants.ACCOUNT_ACTIONTYPE_SUBSCRIBE_SUCCESS);
		flow.setAmount(ct.getBidRequestAmount());
		flow.setNote("认购债权标:" + ct.getBidRequestTitle() + "成功,金额为" + ct.getBidRequestAmount());
		this.flowMapper.insert(flow);
	}

	@Override
	public void createTransferFlow(Account account, CreditTransfer ct) {
		AccountFlow flow = this.createBaseFlow(account);
		flow.setActionType(Constants.ACCOUNT_ACTIONTYPE_TRANSFER_SUCCESS);
		flow.setAmount(ct.getBidRequestAmount());
		flow.setNote("转让债权标:" + ct.getBidRequestTitle() + "成功,回收金额为" + ct.getBidRequestAmount());
		this.flowMapper.insert(flow);
	}

}
